<template>
  <view class="goods_detail-container" v-if="goodsInfo.goods_name">
    <!-- 轮播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item, i) in goodsInfo.pics" :key="i">
        <image :src="item.pics_big" @click="previewImage(i)"></image>
      </swiper-item>
    </swiper>

    <!-- 商品信息区域 -->
    <view class="info-box">
      <view class="price">￥{{goodsInfo.goods_price}}</view>
      <view class="mid-box">
        <view class="goods_name">{{goodsInfo.goods_name}}</view>
        <view class="fav-box">
          <uni-icons type="star" size="24" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <view class="bottom-box">快递：免运费</view>
    </view>

    <!-- 商品详情区域 -->
    <rich-text :nodes="goodsInfo.goods_introduce"></rich-text>

    <!-- 商品导航 -->
    <view class="goods_nav">
      <uni-goods-nav :fill="true" :options="options" :button-group="buttonGroup" @click="leftBtnHandler" @buttonClick="rightBtnHandler" />
      <!-- <button type="default" open-type="contact">客服</button> -->
    </view>
  </view>
</template>

<script>
  import { mapMutations, mapGetters } from 'vuex'

  export default {
    data() {
      return {
        // 商品详情
        goodsInfo: {},
        options: [{
          icon: 'shop',
          text: '店铺'
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]
      }
    },
    onLoad(options) {
      this.getGoodsDetail(options.goods_id)
    },
    methods: {
      ...mapMutations(['addToCart']),
      // 获取商品详情
      async getGoodsDetail(id) {
        const { data: res } = await uni.$http.get('/api/public/v1/goods/detail', { goods_id: id })
        if (res.meta.status !== 200) return uni.$showError()
        // 处理图片的后缀名和图片的样式
        res.message.goods_introduce = res.message.goods_introduce.replace(/webp/g, 'jpg').replace(/<img/g, '<img style="display: block;" ')
        this.goodsInfo = res.message
      },
      // 大图预览图片
      previewImage(i) {
        uni.previewImage({
          current: i,
          urls: this.imageUrls
        })
      },
      // 左侧按钮组的点击事件
      leftBtnHandler(e) {
        // 点击了客服按钮
        if (e.content.text === '店铺') {}
        // 点击了购物车按钮
        if (e.content.text === '购物车') {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      },
      // 右侧按钮组的点击事件
      rightBtnHandler(e) {
        if (e.content.text === '加入购物车') {
          // 获取商品信息，并存储到 Store 中
          // 商品信息应该包含 6 个属性：
          // goods_id, goods_name, goods_price, goods_small_logo, goods_count, is_checked
          const goods = {
            goods_id: this.goodsInfo.goods_id,
            goods_name: this.goodsInfo.goods_name,
            goods_price: this.goodsInfo.goods_price,
            goods_small_logo: this.goodsInfo.goods_small_logo,
            goods_count: 1,
            is_checked: true
          }
          this.addToCart(goods)
        }
      }
    },
    computed: {
      ...mapGetters(['goodsTotal']),
      // 预览图片的 URL 地址
      imageUrls() {
        if (!this.goodsInfo.pics) return []
        return this.goodsInfo.pics.map(x => x.pics_big)
      }
    },
    watch: {
      /* goodsTotal(total) {
         this.options.find((x) => {
           if (x.text === '购物车') {
             x.info = total
           }
         })
       } */
      goodsTotal: {
        handler: function(total) {
          this.options.find((x) => {
            if (x.text === '购物车') {
              x.info = total
            }
          })
        },
        // 该回调将会在侦听开始之后被立即调用
        immediate: true
      }
    }
  }
</script>

<style lang="scss">
  swiper {
    height: 700rpx;

    image {
      width: 100%;
      height: 100%;
    }
  }

  .info-box {
    padding: 10rpx;

    .price {
      font-size: 20px;
      color: #C00000;
      line-height: 40px;
    }

    .mid-box {
      display: flex;
      justify-content: space-between;

      .goods_name {
        font-size: 13px;
        padding-right: 15px;
      }

      .fav-box {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        padding: 0 15px;
        border-left: 1px solid #efefef;
        color: gray;

        text {
          font-size: 12px;
        }
      }
    }

    .bottom-box {
      font-size: 12px;
      color: gray;
      line-height: 24px;
    }
  }

  .goods_nav {
    bottom: 0;
    position: fixed;
    width: 100%;
  }

  .goods_detail-container {
    padding-bottom: 50px;
  }
</style>
